<?php

namespace Admin\Controller;

use Zend\Validator\File\Size;
use Zend\Validator\File\Extension;

class UploadImage {

    public function upload($File, $form, $dirUpload, $fileRename = null) {
        $size = new Size(array('max' => 2000000)); //minimum bytes filesize
        $extention = new Extension(array('jpg', 'JPG', 'png', 'PNG', 'peg', 'PEG'));
        $adapter = new \Zend\File\Transfer\Adapter\Http();
        $adapter->setValidators(array($size, $extention), $File['name']);
        if (!$adapter->isValid()) {
            $dataError = $adapter->getMessages();
            $error = array();
            foreach ($dataError as $key => $row) {
                $error[] = $row;
            }
            $form->setMessages(array('fileupload' => $error));
        } else {
//            $dir_upload = getcwd() . '/public/img/profilePhotos';
            $adapter->setDestination($dirUpload);
            $adapter->addFilter('Rename', array('target' => $dirUpload . '/' . $fileRename, 'overwrite' => true));
            $adapter->receive($File['name']);
        }
    }

}

?>
